package com.cysyz.mylib.hibernatedao;

import com.cysyz.mylib.dao.engine.SQLQuery;
import org.hibernate.transform.Transformers;
import org.hibernate.type.Type;

import java.util.Collection;
import java.util.List;

public class SQLQueryHibernate implements SQLQuery {

		private org.hibernate.SQLQuery sqlQuery;

		public SQLQueryHibernate(org.hibernate.SQLQuery sqlQuery) {
            this.sqlQuery = sqlQuery;
		}
		
		public void setParameter(Integer placeHolder, Object value) {
			this.sqlQuery.setParameter(placeHolder, value);
		}
		
		public int execute() {
			return this.sqlQuery.executeUpdate();
		}

		@SuppressWarnings("rawtypes")
		@Override
		public SQLQuery addEntity(Class clazz) {
			this.sqlQuery.setResultTransformer(Transformers.aliasToBean(clazz));
			return this;
		}

		@SuppressWarnings("rawtypes")
		@Override
		public List getResultList() {
			return this.sqlQuery.list();
		}

		@Override
		public SQLQuery setFirstResult(int firstindex) {
			this.sqlQuery.setFirstResult(firstindex);
			return this;
		}

		@Override
		public SQLQuery setMaxResults(int maxresult) {
			this.sqlQuery.setMaxResults(maxresult);
			return this;
		}

		@Override
		public void setCacheAble(boolean isCacheAble) {
			this.sqlQuery.setCacheable(isCacheAble);
		}

		@Override
		public Object getSingleResult() {
			return this.sqlQuery.uniqueResult();
		}

		@Override
		public String getQueryString() {
			
			return this.sqlQuery.getQueryString();
		}

		public SQLQuery addScalar(String alias, Object type) {
            sqlQuery.addScalar(alias, (Type)type);
			return this;
		}

		public SQLQuery addScalar(String alias) {
            sqlQuery.addScalar(alias);
            return this;
		}

		@Override
		public void setParameterList(String placeHolder, Collection<?> param) {
			sqlQuery.setParameterList(placeHolder, param);
		}

		@Override
		public void setParameterList(String placeHolder, Object[] param) {
			sqlQuery.setParameterList(placeHolder, param);
		}

		@Override
		public void setParameter(String placeHolder, Object value) {
			sqlQuery.setParameter(placeHolder, value);
		}
	}